/*
 * Copyright (c)e 2024-2024.Powered by 清明雨揽月
 * This software is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN 'AS IS' BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include <iostream>
#include "single_linked_list.h"

void single_linked_list::operate()
{
    single_linked_list single_linked_list;
    single_linked_list.init_single_linked_list();
    single_linked_list.list_head_insert();
}

bool single_linked_list::init_single_linked_list()
{
    _link_list = static_cast<l_node *>(malloc(sizeof(l_node)));
    _link_list->next = nullptr;
    return true;
}

int single_linked_list::get_length() const
{
    int len = 0;
    const l_node *p = _link_list;
    while (p->next != nullptr)
    {
        p = p->next;
        len++;
    }
    return len;
}

l_node *single_linked_list::get_elem(const int index) const
{
    l_node *p = _link_list;
    int j = 0;
    while (p != nullptr && j < index)
    {
        p = p->next;
        j++;
    }
    return p;
}

l_node *single_linked_list::locate_elem(const string &data) const
{
    l_node *p = _link_list->next;
    while (p != nullptr && p->data != data)
    {
        p = p->next;
    }
    return p;
}

bool single_linked_list::list_insert(const int index, string data) const
{
    l_node *p = _link_list;
    int j = 0;
    while (p != nullptr && j < index - 1)
    {
        p = p->next;
        j++;
    }
    if (p == nullptr)
        return false;
    auto *s = static_cast<l_node *>(malloc(sizeof(l_node)));
    s->data = std::move(data);
    s->next = p->next;
    p->next = s;
    return true;
}

void single_linked_list::list_tail_insert() const
{
    l_node *r = _link_list;
    for (int i = 0; i <= 10; i++)
    {
        string data;
        std::cout << "请输入第" << i << "个结点值：";
        std::cin >> data;
        const auto s = static_cast<l_node *>(malloc(sizeof(l_node)));
        s->data = std::move(data);
        r->next = s;
        r = s;
    }
    r->next = nullptr;
}

bool single_linked_list::list_delete(const int index, string &data) const
{
    l_node *p = _link_list;
    int j = 0;
    while (p != nullptr && j < index - 1)
    {
        p = p->next;
        j++;
    }
    if (p == nullptr || p->next == nullptr)
        return false;
    l_node *q = p->next;
    data = q->data;
    p->next = q->next;
    free(q);
    return true;
}

void single_linked_list::list_head_insert() const
{
    for (int i = 0; i <= 10; i++)
    {
        auto *s = static_cast<l_node *>(malloc(sizeof(l_node)));
        string data;
        std::cout << "请输入第" << i << "个结点值：";
        std::cin >> data;
        s->data = data;
        s->next = _link_list->next;
        _link_list->next = s;
    }
}
